import request from '../utils/request'
import qs from 'qs';
import {routerRedux} from 'dva/router'
let teacher = {
    namespace:'teacher',
    state:{
        isLogin:false,
        data:[],
    },
    effects:{
        *doLogin({payload},{put,call}){
            //console.log(payload);
            let res =yield call(request('checkLogin',{method:'post',data:qs.stringify(payload)}));
            //console.log(res);
            // yield put({type:'changeData',payload:res.data});
            if(res.data.status != 0){
                return alert('用户名或密码不正确!');
            }
            window.sessionStorage.setItem('user',JSON.stringify(payload));
            yield put({type:'changeLogin',payload:{isLogin:true}});
            //跳转页面
            yield put(routerRedux.push('/home'));
        },
        *getListData({payload},{put,call}){
            //console.log(payload);
            let res = yield call(request('getTeacherData'));
            if(res.data.status !=0){
                return window.alert('网络拥挤，请稍后再试');
            }
            //console.log(res);
            console.log(res.data.message);
            res.data.message.forEach((item)=>{
                if(item.isuse == '1'){
                    item.isuse = true;
                }else{
                    item.isuse = false;
                }
                item.tags = ['loser'];
            })
            yield put({type:'receiveTeacherData',payload:{data:res.data.message}});
        },
        *addNewTeacher({payload},{put,call}){
            //console.log(payload);
            let res = yield call(request('addTeacherData',{method:'post',data:qs.stringify(payload)}));
            if(res.data.status !=0){
                return window.alert('网络拥挤，请稍后再试');
            }
            yield put(routerRedux.push('/home/teacherList'));
            yield put({type:'getListData'});
        },
        *changeIsuse({payload},{put,call}){
            //console.log(payload);
            let res = yield call(request('changeIsuse',{method:'post',data:qs.stringify(payload)}));
            if(res.data.status != 0){
                return window.alert('网络拥挤，请稍后再试');
            }
            yield put({type:'getListData'});
        },
        *deleteItem({payload},{put,call}){
            let res = yield call(request('deleteItem?id='+payload.id));
            if(res.data.status !=0 ){
                return window.alert('网络拥挤，请稍后再试');
            }
            yield put({type:'getListData'});
        }
        
    },
    reducers:{
        changeLogin(state,{payload}){
            return{
                isLogin:payload.isLogin,
            }
        },
        receiveTeacherData(state,{payload}){
            //console.log('1111');
            //console.log(state);
            console.log(payload);
            return{
                data:payload.data
            }
        }
    },
    
}

export default teacher;